<?php
/**
 * ZYSHOP 控制台首页
 * ============================================================================
 * 网络科技有限公司，并保留所有权利。
*/

define('IN_ZYS', true);
require(dirname(__FILE__) . '/includes/init.php');

//品牌列表
if($_REQUEST['act'] == 'list'){
	$sql="select * from ".ly()."brand where 1=1 order by sort_order";
	/*分页开始*/
	$num='10';
	$res=$db->query($sql);
	$total_count=$db->num_rows($res);
	$pagelist = new page(array('total'=>$total_count, 'perpage'=>$num));
	$currenpage=$pagelist->nowindex;
	$start=($currenpage-1)*$num;
	if ($total_count>$num){
		$smarty->assign('page',$pagelist->show(3));
	}else{
		$smarty->assign('page','');
	}
	$limit=" limit ".$start.','.$num;
	/*分页结束*/
	$sql.=$limit;
	$data=$db->getAll($sql);
	$smarty -> assign("brand_list",$data);
	$smarty ->display("brand_list.html");
}elseif($_REQUEST['act'] == 'edit'){
	$b_id=isset($_REQUEST['b_id'])?$_REQUEST['b_id']:0;
	if($_POST){
		$brand_id=isset($_POST['brand_id'])?$_POST['brand_id']:"";//品牌id
		$brand_name=isset($_POST['brand_name'])?$_POST['brand_name']:"";//名称
		$is_show=isset($_POST['is_show'])?$_POST['is_show']:0;//是否显示
		$sort_order=isset($_POST['sort_order'])?$_POST['sort_order']:"";//排序号
		$brand_desc=isset($_POST['brand_desc'])?$_POST['brand_desc']:"";//描述
		$img_url=isset($_POST['img_logo'])?$_POST['img_logo']:"";//品牌已有logo图
		@$logo_imgs=$_FILES['logo']['error'];
		if($logo_imgs==0){
			require("../includes/upload_admin.php");//加载图片上传类
			$up=new upphoto;
			$up->get_ph_tmpname($_FILES['logo']['tmp_name']);
			$up->get_ph_type($_FILES['logo']['type']);
			$up->get_ph_size($_FILES['logo']['size']);
			$up->get_ph_name($_FILES['logo']['name']);
			$up->save();
			$img_url=$up->ph_name;
		}
		
		if($brand_id>0){
			$sql="update ".ly()."brand set brand_name='{$brand_name}',is_show='{$is_show}',sort_order='{$sort_order}',brand_desc='{$brand_desc}',brand_logo='{$img_url}' where brand_id='{$brand_id}'";
		}else{
			$sql="insert into ".ly()."brand(brand_name,is_show,sort_order,brand_desc,brand_logo) values('{$brand_name}','{$is_show}','{$sort_order}','{$brand_desc}','{$img_url}')";
		}
		$rs=$db -> query($sql);
		if($rs){
			$href[] = array('text' => "返回列表", 'href' => 'brand.php?act=list');
			if($brand_id){$href[] = array('text' => "重新编辑", 'href' => 'brand.php?act=edit&b_id='.$brand_id);}
			sys_msg("分类操作成功！",$href);
		}else{
			sys_msg("操作分类失败！");
		}
	}else{
		$sql="select * from ".ly()."brand where brand_id='{$b_id}'";
		$data=$db -> getRow($sql);
		$smarty -> assign("brand",$data);
		$smarty -> display("brand_edit.html");
	}
	
}elseif($_REQUEST['act'] == 'ajax_sale'){
    //显示状态
    $brand_id=isset($_POST['brand_id'])?$_POST['brand_id']:0;
    $sale=isset($_POST['zt'])?$_POST['zt']:0;
    if($sale==1){$zt='0';}else{$zt='1';}
    $sql="update ".ly()."brand set is_show='{$zt}' where brand_id='{$brand_id}'";
    $res=$db->query($sql);
    if($res){
        echo "1";   
    }else{
        echo "2";
    }
}elseif($_REQUEST['act'] == 'ajax_del'){
	//商品分类的排序
	$brand_id=isset($_POST['brand_id'])?$_POST['brand_id']:0;
	$sql="select count(*) from ".ly()."goods where brand_id='{$brand_id}'";
	$res=$db->getAll($sql);
	$num=$res['0']['count(*)'];
	if ($num>0) {
		echo "1";
	}else{
		$sql="delete from ".ly()."brand where brand_id='{$brand_id}'";
        $res=$db->query($sql);
        $a=mysql_affected_rows();
        if($a=='1'){
            echo "2";
        }else{
            echo "3";
        }
	}
}
?>
